/**
 * 责任链模式:类似于工作流。请求者发起请求，处理者依次处理请求，直到有处理者处理请求为止。
 */
public class ChainResponsePattern {
    public static void main(String[] args) {
        Handler handler1 = new Handler("组长",5);
        Handler handler2 = new Handler("经理",10);
        Handler handler3 = new Handler("老总",20);
        handler1.setNextHandler(handler2);
        handler2.setNextHandler(handler3);
        handler1.handleRequest(15);
    }
}
//责任链的处理者:注意，这里可以用抽象类，用组长、经理、老总来表示不同的处理者，也可以用接口来表示。
class Handler{
    private String name;
    private Integer weight;
    private Handler nextHandler;

    public Handler(String name,Integer weight){
        this.name = name;
        this.weight = weight;
    }
    public void setNextHandler(Handler nextHandler){
        this.nextHandler = nextHandler;
    }
    public void handleRequest(int request){
        if(this.handlerRequest(request)){
            System.out.println(this.name + "处理请求");
            return;
        }
        if(this.nextHandler != null){
            this.nextHandler.handleRequest(request);
        }
    }
    protected boolean handlerRequest(int request){
        return  request <= this.weight;
    }
}
abstract class HandlerNew{
    private HandlerNew nextHandler;

    public void setNextHandler(HandlerNew nextHandler){
        this.nextHandler = nextHandler;
    }
    //抽象方法用于处理请求
    public  abstract void handleRequest(int request);
}